<template>
  <div class="app-container">
    <el-card class="filter-container" shadow="never">
      <div>
        <el-button style="float:right" type="primary" @click="handleSearchList()" size="mini">
          查询搜索
        </el-button>
        <el-button style="float:right;margin-right: 15px" @click="handleResetSearch()" size="mini">
          重置
        </el-button>
      </div>
      <div>
        <el-form :inline="true" :model="listQuery" size="mini" label-width="100px">
          <el-form-item label="订单号：">
            <el-input v-model="listQuery.orderNo" class="input-width" placeholder="请输入订单号" clearable></el-input>
          </el-form-item>
          <el-form-item label="用户名称：">
            <el-autocomplete v-model="listQuery.memberName" placeholder="请输入用户名称" :fetch-suggestions="querySearchAsync"
              @select="handleSelect"></el-autocomplete>
          </el-form-item>

          <el-form-item label="发起时间：">
            <el-date-picker v-model="listQuery.date" type="daterange" range-separator="至" clearable
              start-placeholder="开始日期" end-placeholder="结束日期">
            </el-date-picker>
          </el-form-item>

        </el-form>
      </div>
    </el-card>
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>提现管理</span>
      <!-- <el-button size="mini" type="success" class="btn-add" @click="handleExport"
        style="margin-left: 20px">导出</el-button> -->
    </el-card>
    <div class="table-container case-container" >
      <el-table ref="adminTable" size="mini" :data="list" style="width: 100%;" v-loading="listLoading" border>
        <el-table-column label="订单号" width="180" align="center">
          <template slot-scope="scope">{{ scope.row.orderNo }}</template>
        </el-table-column>
        <el-table-column label="用户信息" width="160">
          <template slot-scope="scope">
            <div class="list-item">
              手机号码：<span class="green">{{ scope.row.phoneNo }}</span>
            </div>
            <div class="list-item">
              用户昵称：<span class="green">{{ scope.row.nickname }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="提现金额" width="120" align="center">
          <template slot-scope="scope">{{ formatAmount(scope.row.amount) }}</template>
        </el-table-column>
        <el-table-column label="手续费" width="100" align="center">
          <template slot-scope="scope">{{ formatAmount(scope.row.handlingFee) }}</template>
        </el-table-column>
          <el-table-column label="手续费率" width="100" align="center">
          <template slot-scope="scope">{{ parseFloat(scope.row.handlingRate * 100).toFixed(2) }}%</template>
        </el-table-column>
        <el-table-column label="实际到账" align="center">
          <template slot-scope="scope">{{ formatAmount(scope.row.amount-scope.row.handlingFee) }}</template>
        </el-table-column>
        <el-table-column label="账户信息" width="220">
          <template slot-scope="scope">
            <div class="list-item">
              提款方式：<span class="green">{{ scope.row.method }}</span>
            </div>
            <div class="list-item">
              币种：<span class="green">{{ scope.row.currency }}</span>
            </div>
            <div class="list-item">
              网络：<span class="green">{{ scope.row.network }}</span>
            </div>
            <div class="list-item">
              钱包地址：<span class="green">{{ scope.row.address }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="发起时间" width="150" align="center">
          <template slot-scope="scope">{{ scope.row.createTime | formatDateTime }}</template>
        </el-table-column>
        <el-table-column label="处理时间" width="150" align="center">
          <template slot-scope="scope">{{ scope.row.updateTime | formatDateTime }}</template>
        </el-table-column>
        <el-table-column label="订单状态" width="80" align="center">
          <template slot-scope="scope">{{ caseState[scope.row.state] }}</template>
        </el-table-column>
        <el-table-column label="操作" width="120" align="center">
          <template slot-scope="scope">
            <el-button size="mini" type="text" v-if="scope.row.state === 0" @click="handleOption(scope.row, 1)">通过</el-button>
            <el-button size="mini" type="text" v-if="scope.row.state === 0" @click="handleOption(scope.row, 0)">驳回</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination-container">
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper" :current-page.sync="listQuery.pageNum"
        :page-size="listQuery.pageSize" :page-sizes="[5, 10, 15, 20]" :total="total">
      </el-pagination>
    </div>
  </div>
</template>
<script>
import { getWithdrawList,getMemberLickList,passWithdrawReview,rejectWithdrawReview } from '@/api/transaction/index';
import { formatDate } from '@/utils/date';
import { formatAmount } from "@/utils/index";
import { createNamespacedHelpers } from "vuex";
const { mapState } = createNamespacedHelpers("transaction");
const defaultListQuery = {
  pageNum: 1,
  pageSize: 10,
  orderNo: null,
  memberId: null,
  memberName: null,
  date: []

};
export default {
  name: 'caseList',
  data() {
    return {
      listQuery: Object.assign({}, defaultListQuery),
      list: null,
      total: null,
      ids: [],
      row: {},
      listLoading: false,
      deductionVisible: false,
      ordersVisible: false
    }
  },
  computed: {
    ...mapState({
      caseState: state => state.routes.caseState
    })
  },
  created() {
    this.getList();
  },
  filters: {
    formatDateTime(time) {
      if (time == null || time === '') {
        return 'N/A';
      }
      let date = new Date(time);
      return formatDate(date, 'yyyy-MM-dd hh:mm:ss')
    }
  },
  methods: {
    formatAmount,
    handleResetSearch() {
      this.listQuery = Object.assign({}, defaultListQuery);
      this.getList();
    },
    handleSearchList() {
      this.listQuery.pageNum = 1;
      this.getList();
    },
    handleSizeChange(val) {
      this.listQuery.pageNum = 1;
      this.listQuery.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.pageNum = val;
      this.getList();
    },

    //导出
    handleExport() {

    },

    //远程搜索会员id匹配用户名称
    async querySearchAsync(queryString, cb) {
      if (queryString) {
        const res = await getMemberLickList({ nickname: queryString });
        cb(res.data.map(item => ({ value: item.nickname, id: item.id })));
      }
    },
    createStateFilter(queryString) {
      return (state) => {
        return (state.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0);
      };
    },
    //选中内容
    handleSelect(item) {
      this.listQuery.memberName = item.value;
      this.listQuery.memberId = item.id;
    },

    //操作
    handleOption(row, type) {
      this.$confirm(`是否要${type == 1 ? '通过' : '驳回'}`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
          if (type === 1) {
          await passWithdrawReview({ id: row.id });
        } else {
          await rejectWithdrawReview({ id: row.id });
        }
        this.$message({
          type: 'success',
          message: '操作成功!'
        });
        this.getList();
      });
    },
    getList() {
      this.listLoading = true;
      let params = {...this.listQuery};
      delete params.memberName;
      if(params.date&&params.date.length>0){
        params.startTime = formatDate(params.date[0],"yyyy-MM-dd hh:mm:ss");
        params.endTime = formatDate(params.date[1],"yyyy-MM-dd hh:mm:ss");
      }
      delete params.date;
      getWithdrawList(params).then(response => {
        this.listLoading = false;
        this.list = response.data.list;
        this.total = response.data.total;
      });
    }


  }
}
</script>
<style lang="scss">
.case-container{
 height: calc(100vh - 242px);
  .el-table__body-wrapper{
    height: calc(100vh - 295px);
    overflow: auto;
  }
}
</style>